package com.example.fresh_market.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.fresh_market.R;
import com.example.fresh_market.databinding.ActivityMainBinding;
import com.example.fresh_market.fragments.CartFragment;
import com.example.fresh_market.fragments.MainFragment;
import com.example.fresh_market.fragments.MineFragment;
import com.example.fresh_market.fragments.OrderFragment;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private MainFragment fg_order; //点单界面
    private Fragment fg_shop; //购物车界面
    private Fragment fg_bill; //查看历史订单界面
    private Fragment fg_mine; //查看历史订单界面
    private FragmentManager fManager;
    private FrameLayout ly_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        fManager = getSupportFragmentManager();
        RadioGroup radioGroup = binding.rgTab;
        ly_content = binding.lyContent;
        radioGroup.setOnCheckedChangeListener(this::onCheckedChanged); //对下方导航栏设置监听，实现界面切换
        //获取第一个单选按钮，并设置其为选中状态
        RadioButton rb_order = binding.rbOrder;
        rb_order.setChecked(true);
        defaultFragment(); //将点单界面设为默认界面
    }

    public void defaultFragment() {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        fg_order = new MainFragment();
        fTransaction.add(ly_content.getId(), fg_order);
        fTransaction.show(fg_order);
        fTransaction.commitNow();
    }

    @SuppressLint("NonConstantResourceId")
    public void onCheckedChanged(RadioGroup group, int checkedId) { //界面切换函数
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction); //先隐藏所有界面
        switch (checkedId) {
            case R.id.rb_order:
                if (fg_order == null) {
                    fg_order = MainFragment.newInstance();
                    fTransaction.add(R.id.ly_content, fg_order);
                    fTransaction.show(fg_order);
                } else {
                    fTransaction.show(fg_order);
                }
                break;
            case R.id.rb_bill:
                if (fg_bill == null) {
                    fg_bill = OrderFragment.newInstance();
                    fTransaction.add(R.id.ly_content, fg_bill);
                    fTransaction.show(fg_bill);
                } else
                    fTransaction.show(fg_bill);
                break;
            case R.id.rb_shop:
                if (fg_shop == null) {
                    fg_shop = CartFragment.newInstance();
                    fTransaction.add(R.id.ly_content, fg_shop);
                    fTransaction.show(fg_shop);
                } else
                    fTransaction.show(fg_shop);
                break;
            case R.id.rb_mine:
                if (fg_mine == null) {
                    fg_mine = MineFragment.newInstance();
                    fTransaction.add(R.id.ly_content, fg_mine);
                    fTransaction.show(fg_mine);
                } else {
                    fTransaction.show(fg_mine);
                }
                break;
        }
        fTransaction.commitNow();
    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (fg_order != null) fragmentTransaction.hide(fg_order);
        if (fg_bill != null) fragmentTransaction.hide(fg_bill);
        if (fg_shop != null) fragmentTransaction.hide(fg_shop);
        if (fg_mine != null) fragmentTransaction.hide(fg_mine);
    }
}